/**
 * Created by CAOTE on 11/6/2017.
 */
require('../model/domainModel.js');
var mongoose = require('mongoose');
var domainModel = mongoose.model('Domain');
var dateUtil = require('../util/dateUtil');
var _ = require('lodash');
exports.updateDomain = updateDomain;
exports.getDomain = getDomain;
function updateDomain(data, callback){
    if(_.isEmpty(data)){
        return;
    }
    else if(data.type === 'New'){
        createNewDomain(data.domain,callback);
    }
    else if(data.type === 'Delete'){
        deleteDomain(data.name,callback)

    }
}
function createNewDomain(data, callback) {
    data.updateDate = dateUtil.getNowFormatDate();
    domainModel.create(data, function(err, result){
        if(err){
            callback(err, null);
        }else{
            callback(null, result)
        }
    })
}

function getDomain(callback){
    domainModel.find({}, function(err, result){
        if(err){
            console.log(err);
        }else{
            callback(err, result);
        }
    })
}



function deleteDomain(domainName, callback){
    domainModel.remove({domainName: domainName}, function(err,result){
        if(err){
            callback(err, null);
        }else{
            callback(null, result)
        }
    });
}